#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include<string.h>

void PrintArr(int* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}

void MergeSort(int* a, int* tmp, int begin, int end)
{
	if (begin >= end)
		return;
	int mid = (begin + end) / 2;
	MergeSort(a, tmp, begin, mid);
	MergeSort(a, tmp, mid + 1, end);
	int begin1 = begin;
	int begin2 = mid+1;
	int len = 0;
	while (begin1 <= mid && begin2 <= end)
	{
		if (a[begin1] <= a[begin2])
		{
			tmp[len++] = a[begin1++];
		}
		else
		{
			tmp[len++] = a[begin2++];
		}
	}
	while (begin1 <= mid)
	{
		tmp[len++] = a[begin1++];
	}
	while (begin2 <= end)
	{
		tmp[len++] = a[begin2++];
	}
	memcpy(a + begin, tmp, sizeof(int)*(end-begin+1));
}

int main()
{
	int arr[11] = { 9,5,6,7,8,9,1,2,3,4 };
	int* tmp = (int*)malloc(11 * sizeof(int));
	MergeSort(arr, tmp, 0, 10);

	PrintArr(arr, sizeof(arr) / sizeof(arr[0]));
	return 0;
}